package com.example.tnot.Teacher.CoursePart.CourseInternal.ClassDetail;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.viewpager2.widget.ViewPager2;

import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.tnot.R;
import com.example.tnot.Teacher.BaseFragment.BaseBackFragment;
import com.example.tnot.Teacher.CoursePart.CourseInternal.ClassDetail.Adapter.vpClassDetailAdapter;
import com.example.tnot.Util.popupwindow.FloatWindowService;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class ClassDetailFragment extends BaseBackFragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;
    View view;
    TabLayout tl_ClassDetail;
    ViewPager2 vp_ClassDetail;
    TextView tv_CourseName;
    ImageView back;
    BroadcastReceiver floatWindowReceiver;

    public ClassDetailFragment() {
    }
    public static ClassDetailFragment newInstance(String param1, String param2) {
        ClassDetailFragment fragment = new ClassDetailFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.course_class_detail_fragment, container, false);
        tl_ClassDetail = view.findViewById(R.id.tl_ClassDetail);
        vp_ClassDetail = view.findViewById(R.id.vp_ClassDetail);
        vpClassDetailAdapter adapter = new vpClassDetailAdapter(getActivity());
        vp_ClassDetail.setAdapter(adapter);
        // 将TabLayout与ViewPager2联动
        new TabLayoutMediator(tl_ClassDetail, vp_ClassDetail,
                (tab, position) -> tab.setText(adapter.tabs[position])
        ).attach();

//        获取bundle
        Bundle bundle = getArguments();
        String className = bundle.getString("className");
        String courseName = bundle.getString("courseName");
        String jumpType = bundle.getString("jumpType");//获取跳转前置页面，教学表示从我教的课点击班级进入，学习表示从我学的课点击课程进入

        if (jumpType.equals("教学")){
            tv_CourseName = view.findViewById(R.id.tv_CourseName);
            tv_CourseName.setText(className);
        }else if (jumpType.equals("学习")){
            tv_CourseName = view.findViewById(R.id.tv_CourseName);
            tv_CourseName.setText(courseName);
        }else {
            // 抛出异常
            throw new IllegalArgumentException("未知的跳转类型: " + jumpType);
        }

        back = view.findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController navController = Navigation.findNavController(v);
                navController.navigateUp();
            }
        });

        return view;
    }

}